package com.springboot.admin.controller;

import com.springboot.admin.common.ResponseMap;
import com.springboot.admin.entity.CateDto;
import com.springboot.admin.entity.NavigationDto;
import com.springboot.admin.service.AdminService;
import com.springboot.admin.service.NavigationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/nav")
@Api(tags = "导航相关接口")
public class NavigationController {

  @Autowired
  NavigationService navigationService;
  @Autowired
  AdminService adminService;


  @ApiOperation(value="获取导航列表接口")
  @PostMapping("/getList")
  public ResponseMap getList() {
    // 按分类查询导航
    return ResponseMap.success(navigationService.getAll());
  }

  @ApiOperation(value="导航详情接口")
  @GetMapping("/getDetail")
  public ResponseMap getOne(@RequestParam int id) {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", navigationService.getDetail(id));
    return ResponseMap.success(resultMap);
  }

  @ApiOperation(value="添加导航接口")
  @PostMapping("/addItem")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap inertOne(@RequestBody NavigationDto navigationDto) {
    navigationService.insertOne(navigationDto);
    return ResponseMap.success();
  }

  @ApiOperation(value="删除导航接口")
  @PostMapping("/deleteItem")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap delOne(@RequestBody NavigationDto navigationDto) {
    navigationService.delOne((int)navigationDto.getId());
    return ResponseMap.success();
  }

  @ApiOperation(value="更新导航接口")
  @PostMapping("/updateItem")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateOne(@RequestBody NavigationDto navigationDto) {
    navigationService.updateOne(navigationDto);
    return ResponseMap.success();
  }

  @ApiOperation(value="新增导航分类接口")
  @PostMapping("/insertCate")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap inertOne(@RequestBody CateDto cateDto) {
    navigationService.insertCate(cateDto);
    return ResponseMap.success();
  }

  @ApiOperation(value="获取导航分类接口")
  @GetMapping("/getCate")
  public ResponseMap getCate() {
    return ResponseMap.success(navigationService.getCate());
  }

  @ApiOperation(value="更新导航分类接口")
  @PostMapping("/updateCate")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateCate(@RequestBody CateDto cateDto) {
    navigationService.updateCate(cateDto);
    return ResponseMap.success();
  }

  @ApiOperation(value="删除导航分类接口")
  @PostMapping("/delCate")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap delCate(@RequestBody CateDto cateDto) {
    navigationService.delCate(cateDto.getId());
    return ResponseMap.success();
  }
  @PostMapping("/admin/getList")
  @ApiOperation(value="获取导航列表接口")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getAllList(@RequestBody Map map) {
    // 后台管理查看所有导航数据
    return ResponseMap.success(adminService.getNavList(map));
  }
}
